 <template>
  <div style="height: 100%">
    <el-tabs v-model="activeName" @tab-click="changeTab">
      <el-tab-pane label="发起，已签订" name="CONFIRM">
        <ViewList :tableData="tableData"></ViewList>
      </el-tab-pane>

      <el-tab-pane label="发起，未应答" name="PENDING">
        <ViewList :tableData="tableData" :isRecall="isRecall" @recall="recall"></ViewList>
      </el-tab-pane>

      <el-tab-pane label="发起，被拒绝" name="REJECTED">
        <ViewList :tableData="tableData"></ViewList>
      </el-tab-pane>

      <el-tab-pane label="被邀约，已应答" name="CONFIRM_Processed">
        <ViewList :tableData="tableData"></ViewList>
      </el-tab-pane>

      <el-tab-pane label="被邀约，已拒绝" name="CONFIRM_REJECTED">
        <ViewList :tableData="tableData"></ViewList>
      </el-tab-pane>
    </el-tabs>

    <Pagination :total="total" @handleChange="handleChange"/>
  </div>
</template>

<script>
import ViewList from "./viewList.vue";
import { getApplyOrgId, getConfirmOrgId, handleContract } from "api/getData";
import { formatDateTime } from "src/util/time.js";
import Pagination from "src/components/pagination";

export default {
  data() {
    return {
      activeName: "CONFIRM",
      isRecall: true,
      tableData: [],
      currentName: "CONFIRM", // 当前请求的列表

      total: -1
    };
  },
  components: { ViewList, Pagination },
  mounted() {
    this.getData("CONFIRM", 1);
  },
  methods: {
    changeTab(tab, event) {
      this.getData(tab.name, 1);
      this.currentName = tab.name;
    },
    getData(name, pageNumber) {
      if (name === "CONFIRM_Processed") {
        getConfirmOrgId("CONFIRM", pageNumber).then(res => {
          this.tableData = this.handleData(res.content);

          this.total = res.totalElements;
        });
      } else if (name === 'CONFIRM_REJECTED') {
        getConfirmOrgId("REJECTED", pageNumber).then(res => {
          this.tableData = this.handleData(res.content);

          this.total = res.totalElements;
        });
      } else {
        getApplyOrgId(name, pageNumber).then(res => {
          this.tableData = this.handleData(res.content);

          this.total = res.totalElements;
        });
      }
    },
    recall: function(index) {
      handleContract(this.tableData[index].id, "WITHDRAW").then(res => {
        this.$message.success("成功撤回！");
        getApplyOrgId("PENDING").then(res => {
          this.tableData = this.handleData(res.content);
        });
      });
    },
    handleData: function(data) {
      return data.map(item => {
        item.createTime = formatDateTime(item.createTime);
        item.handleTime = item.handleTime
          ? formatDateTime(item.handleTime)
          : "未签署";
        return item;
      });
    },
    handleChange(pageNumber) {
      this.getData(this.currentName, pageNumber);
    }
  }
};
</script>

<style lang="scss" scoped>
@import "src/style/mixin";
.tabList {
  @include fj(flex-start);

  .tabItem {
    padding-left: 20px;
  }
}
</style>
